package com.kye.express.business.jd.search_list.search;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.beloo.widget.chipslayoutmanager.ChipsLayoutManager;
import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.base.Logger;
import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatActivity;
import com.kye.express.business.jd.search_list.search_result.JDGoodsSearchResultActivity;
import com.kye.express.databinding.ActivityJdSearchBinding;
import com.kye.express.utils.ActivityManager;
import com.kye.express.utils.InputUtils;

import java.util.List;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * 京东兑换-搜索页面 Activity
 * <p>
 * Created by SkyYu on 2018/1/28
 */
public class JDGoodsSearchActivity extends BaseAppCompatActivity implements JDGoodsSearchContract.JDGoodsSearchView {

    private ActivityJdSearchBinding mBinding;
    private JDGoodsSearchContract.JDGoodsSearchPresenter mPresenter;
    private MultiTypeAdapter mSearchHistoryAdapter;

    public static void startActivity() {
        ActivityManager.getInstance().getCurrentActivity().startActivity(new Intent(ActivityManager.getInstance().getCurrentActivity(), JDGoodsSearchActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViews();
        initData();
        initEvents();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mPresenter) mPresenter.unbind();
    }

    private void initViews() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_jd_search);
    }

    private void initData() {
        initPresenter();
        initSearchHistoryView();
    }

    private void initPresenter() {
        mPresenter = new JDGoodsSearchContract.JDGoodsSearchPresenter();
        mPresenter.bind(this);
    }

    private void initSearchHistoryView() {
        ChipsLayoutManager spanLayoutManagerHistory = ChipsLayoutManager.newBuilder(this)
                .setOrientation(ChipsLayoutManager.HORIZONTAL)
                .build();
        mBinding.recyclerJdSearchHistoryLayout.setLayoutManager(spanLayoutManagerHistory);
        mSearchHistoryAdapter = new MultiTypeAdapter();
        mSearchHistoryAdapter.register(String.class, new JDGoodsSearchItemViewHolder(new JDGoodsSearchItemViewHolder.OnItemListener() {
            @Override
            public void onItemClicked(String item) {
                onLabelClicked(item);
            }
        }));
        mBinding.recyclerJdSearchHistoryLayout.setAdapter(mSearchHistoryAdapter);
        mPresenter.updateSearchHistoryData();
    }

    private void initEvents() {
        RxTextView.textChanges(mBinding.edittextJdSearchBarInput)
                .map(new Function<CharSequence, Boolean>() {
                    @Override
                    public Boolean apply(CharSequence charSequence) throws Exception {
                        return TextUtils.isEmpty(charSequence);
                    }
                })
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean isEmpty) throws Exception {
                        if (isEmpty) {
                            mBinding.buttonJdSearchBarGo.setEnabled(false);
                        } else {
                            mBinding.buttonJdSearchBarGo.setEnabled(true);
                        }
                    }
                });

        RxView.clicks(mBinding.buttonJdSearchBarGo)
                .compose(new DebounceObservableTransformer<>())
                .compose(this.bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        String searchResult = mBinding.edittextJdSearchBarInput.getText().toString();
                        mPresenter.saveQueryDataObservable(searchResult);
                        JDGoodsSearchResultActivity.startActivity(searchResult);
                    }
                });

        RxView.clicks(mBinding.buttonJdSearchHistoryClear)
                .compose(new DebounceObservableTransformer<>())
                .compose(this.bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        mPresenter.clearSearchHistoryData();
                    }
                });
    }

    @Override
    public void onLabelClicked(String searchResult) {
        InputUtils.hideSoftInput(this);
        mBinding.edittextJdSearchBarInput.setText(searchResult);
        mBinding.edittextJdSearchBarInput.setSelection(searchResult.length());
        mBinding.edittextJdSearchBarInput.clearFocus();
        mPresenter.saveQueryDataObservable(searchResult);
        JDGoodsSearchResultActivity.startActivity(searchResult);
    }

    @Override
    public void updateQueryRecordSuccess(List<String> data) {
        mSearchHistoryAdapter.setItems(data);
        mSearchHistoryAdapter.notifyDataSetChanged();
    }

    @Override
    public void updateQueryHistoryFailed(Throwable e) {
        Logger.d("JDGoodsSearchActivity - updateQueryHistoryFailed : " + e.getMessage());
    }
}